{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bc7e574f",
   "metadata": {},
   "source": [
    "# 1. Python使用API模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "81f30960",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "501e8e70",
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "5d79367f",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = 'SHa1fNTCU7OPp6FMyBRYK12WzvL1isni'\n",
    "API_secret = 'Mn26GFOfOwA5LDjzgGJS-Ee9bU6Is26v'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "268eb7e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "a58cee51",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "05f72029",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "2c01d33c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1648451590,b8f4e58c-9fc4-4da6-9225-7ec56d062bcf\",\"time_used\":247,\"faces\":[{\"face_token\":\"085403a3b560a0cd01a2521b3f26d88f\",\"face_rectangle\":{\"top\":108,\"left\":141,\"width\":107,\"height\":107}}],\"image_id\":\"JoYdaZ0h1hGb4ih1d2EZVw==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "35a9774f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'085403a3b560a0cd01a2521b3f26d88f'"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "a605a90e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'085403a3b560a0cd01a2521b3f26d88f'"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token = r.json()['faces'][0]['face_token']\n",
    "face_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64446ea9",
   "metadata": {},
   "source": [
    "# 2.人脸分析API\n",
    "* 使用模板合理调用参数实现人脸分析API\n",
    "* 获取return_attributes的所有参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "07ae3227",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "analyze_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "\n",
    "# 2.api账户/通行证\n",
    "API_key = 'SHa1fNTCU7OPp6FMyBRYK12WzvL1isni'\n",
    "API_secret = 'Mn26GFOfOwA5LDjzgGJS-Ee9bU6Is26v'\n",
    "\n",
    "# 3.请求方法：POST\n",
    "\n",
    "# 4.准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'face_tokens':face_token,\n",
    "    'return_attributes': 'gender,age,smiling,headpose,blur,beauty'\n",
    "}\n",
    "# 5.发起请求\n",
    "r = requests.post(url = analyze_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "ed51f009",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "1029b93f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 148,\n",
       " 'request_id': '1648451591,5b39cacd-cbee-4dcc-9cde-c5abfd1d9489',\n",
       " 'faces': [{'attributes': {'headpose': {'yaw_angle': 0.004189467,\n",
       "     'pitch_angle': 16.763775,\n",
       "     'roll_angle': 2.3050764},\n",
       "    'beauty': {'female_score': 56.631, 'male_score': 56.694},\n",
       "    'smile': {'threshold': 50.0, 'value': 100.0},\n",
       "    'gender': {'value': 'Male'},\n",
       "    'age': {'value': 53},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.364},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.364},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.364}}},\n",
       "   'face_rectangle': {'width': 107, 'top': 108, 'left': 141, 'height': 107},\n",
       "   'face_token': '085403a3b560a0cd01a2521b3f26d88f'}]}"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "a9ed82c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab968200",
   "metadata": {},
   "source": [
    "# 1.数据处理 pandas.json_normalize()实现数据表格化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "d40a6613",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>085403a3b560a0cd01a2521b3f26d88f</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.yaw_angle</th>\n",
       "      <td>0.004189</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.pitch_angle</th>\n",
       "      <td>16.763775</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.roll_angle</th>\n",
       "      <td>2.305076</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.female_score</th>\n",
       "      <td>56.631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.male_score</th>\n",
       "      <td>56.694</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.age.value</th>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.value</th>\n",
       "      <td>0.364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.value</th>\n",
       "      <td>0.364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.value</th>\n",
       "      <td>0.364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>107</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>107</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                       0\n",
       "face_token                              085403a3b560a0cd01a2521b3f26d88f\n",
       "attributes.headpose.yaw_angle                                   0.004189\n",
       "attributes.headpose.pitch_angle                                16.763775\n",
       "attributes.headpose.roll_angle                                  2.305076\n",
       "attributes.beauty.female_score                                    56.631\n",
       "attributes.beauty.male_score                                      56.694\n",
       "attributes.smile.threshold                                          50.0\n",
       "attributes.smile.value                                             100.0\n",
       "attributes.gender.value                                             Male\n",
       "attributes.age.value                                                  53\n",
       "attributes.blur.blurness.threshold                                  50.0\n",
       "attributes.blur.blurness.value                                     0.364\n",
       "attributes.blur.motionblur.threshold                                50.0\n",
       "attributes.blur.motionblur.value                                   0.364\n",
       "attributes.blur.gaussianblur.threshold                              50.0\n",
       "attributes.blur.gaussianblur.value                                 0.364\n",
       "face_rectangle.width                                                 107\n",
       "face_rectangle.top                                                   108\n",
       "face_rectangle.left                                                  141\n",
       "face_rectangle.height                                                107"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r.json()['faces']).T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54a5e291",
   "metadata": {},
   "source": [
    "# 3.人脸对比\n",
    "* 思考：人脸对比至少需要几张脸？\n",
    "\n",
    "#### 1.人脸检测获取token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "a3acf64e",
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "f3057841",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = 'SHa1fNTCU7OPp6FMyBRYK12WzvL1isni'\n",
    "API_secret = 'Mn26GFOfOwA5LDjzgGJS-Ee9bU6Is26v'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "77caf864",
   "metadata": {},
   "outputs": [],
   "source": [
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "fc8fec46",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "73d63f28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'81df63460ecf35a3141c4e6b9b664326'"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token01 = r.json()['faces'][0]['face_token']\n",
    "face_token01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "70912d98",
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "fd6e98a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = 'SHa1fNTCU7OPp6FMyBRYK12WzvL1isni'\n",
    "API_secret = 'Mn26GFOfOwA5LDjzgGJS-Ee9bU6Is26v'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "cf58e0a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://www.52fb.cn/wp-content/uploads/2018/07/1-131213150J7.jpg'\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "9280f041",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "70b51d33",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'fdfcfe59f0412d6d99570e600c433171'"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token02 = r.json()['faces'][0]['face_token']\n",
    "face_token02"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "895b1925",
   "metadata": {},
   "source": [
    "# 2.人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "d26a9777",
   "metadata": {},
   "outputs": [],
   "source": [
    "compare_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83f27aee",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = 'SHa1fNTCU7OPp6FMyBRYK12WzvL1isni'\n",
    "API_secret = 'Mn26GFOfOwA5LDjzgGJS-Ee9bU6Is26v'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "53fb8f57",
   "metadata": {},
   "outputs": [],
   "source": [
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'face_token1':face_token01,\n",
    "    'face_token2':face_token02,\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "23b4c059",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(url = compare_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "36be0854",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "caaa5002",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648451942,9ef01213-3df2-4621-8b99-8bb613809165',\n",
       " 'time_used': 505,\n",
       " 'confidence': 30.64,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7690b344",
   "metadata": {},
   "source": [
    "# 4.封装人脸检测函数，实现快速调用\n",
    "* 准备工作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "0eb8c58e",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = 'SHa1fNTCU7OPp6FMyBRYK12WzvL1isni'\n",
    "API_secret = 'Mn26GFOfOwA5LDjzgGJS-Ee9bU6Is26v'\n",
    "img_url_01 = 'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'\n",
    "img_url_02 = 'https://www.52fb.cn/wp-content/uploads/2018/07/1-131213150J7.jpg'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "ff33df49",
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_secret,img_url):\n",
    "    \"\"\"face++ API人脸检测函数\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_secret,\n",
    "        'image_url':img_url\n",
    "    }\n",
    "    r = requests.post(url = base_url, params = payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "a6dd50f4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648452410,f23017ce-0df3-4c5f-8cda-e15b2a401571',\n",
       " 'time_used': 449,\n",
       " 'faces': [{'face_token': 'b0947c1b9733cf3dfab062e3a9e519bb',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_secret,img_url_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "4f4b1e3f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648452421,e12b0614-2a57-4582-85da-a0fa59983afc',\n",
       " 'time_used': 336,\n",
       " 'faces': [{'face_token': 'befa9d1ca6fbba76cbe6c118406cd9d0',\n",
       "   'face_rectangle': {'top': 175, 'left': 320, 'width': 229, 'height': 229}}],\n",
       " 'image_id': 'kVL9nQazZGIEQw5ZW3HHRw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_secret,img_url_02)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a6bc59c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
